import { ApiProperty } from '@nestjs/swagger';
import {
  Column,
  Entity,
  Index,
  JoinColumn,
  JoinTable,
  ManyToMany,
  ManyToOne,
  Tree,
  TreeChildren,
  TreeParent,
} from 'typeorm';

import { CommonEntity } from '~/common/entity/common.entity';

import { DictTypeEntity } from '../dict-type/dict-type.entity';
import { UserEntity } from '~/modules/user/user.entity';

@Entity({ name: 'ah_label_item' })
@Index(['id'])
@Tree('materialized-path')
export class DictItemEntity extends CommonEntity {
  @ManyToOne(() => DictTypeEntity, (dictType) => dictType.items, { onDelete: 'CASCADE' })
  @JoinColumn({ name: 'type_id' })
  type: DictTypeEntity;

  @Column({ type: 'varchar', length: 50 })
  @ApiProperty({ description: '字典项键名' })
  label: string;

  @Column({ type: 'varchar', length: 50 })
  @ApiProperty({ description: '字典项值' })
  value: string;

  @Column({ nullable: true, comment: '字典项排序' })
  orderNo: number;

  @Column({ type: 'tinyint', default: 1 })
  @ApiProperty({ description: ' 状态' })
  status: number;

  @Column({ type: 'varchar', nullable: true })
  @ApiProperty({ description: '备注' })
  remark: string;

  @TreeParent()
  parent?: DictItemEntity;

  @TreeChildren({ cascade: true })
  children: DictItemEntity[];

	// @ManyToMany
	// 多对多关系，关联用户
	@ManyToMany(() => UserEntity, (user) => user.labels)
	@JoinTable({
		name: 'ah_label_user',
		joinColumn: { name: 'label_id' },
		inverseJoinColumn: { name: 'user_id' },
	})
	users: UserEntity[];
}
